package com.jwmp.rbac.service.impl;

import com.jwmp.core.util.MyBatisUtil;
import com.jwmp.core.util.PageResult;
import com.jwmp.core.util.QueryObject;
import com.jwmp.rbac.domain.Department;
import com.jwmp.rbac.domain.Employee;
import com.jwmp.rbac.mapper.DepartmentMapper;
import com.jwmp.rbac.mapper.EmployeeMapper;
import com.jwmp.rbac.service.IDepartmentService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description TODO
 * @Version 1.0
 */
public class DepartmentServiceImpl implements IDepartmentService {
    public void insert(Department dept) {
        SqlSession session = MyBatisUtil.openSession();
        DepartmentMapper mapper = session.getMapper(DepartmentMapper.class);
        mapper.insert(dept);
        session.commit();
        session.close();
    }

    public void update(Department dept) {
        SqlSession session = MyBatisUtil.openSession();
        DepartmentMapper mapper = session.getMapper(DepartmentMapper.class);
        mapper.update(dept);
        session.commit();
        session.close();

    }

    public void updateStatus(Long id,Boolean status) {
        SqlSession session = MyBatisUtil.openSession();
        DepartmentMapper mapper = session.getMapper(DepartmentMapper.class);
        mapper.updateStatus(id,status);
        session.commit();
        session.close();
    }

    public PageResult selectList(QueryObject qo){
        return selectList(qo,null);
    }

    public PageResult selectList(QueryObject qo,Boolean status) {
        SqlSession session = MyBatisUtil.openSession();
        DepartmentMapper mapper = session.getMapper(DepartmentMapper.class);
        int totalCount = mapper.selectCount(qo);
        if (totalCount <= 0) {
            session.close();
            return  new PageResult(qo.getCurrentPage(),qo.getPageSize());
        }
        List<Department> departmentList = mapper.selectList(qo,status);
        session.commit();
        session.close();
        return new PageResult(qo.getCurrentPage(),qo.getPageSize(),departmentList,totalCount);
    }

    public Department selectOne(Long id) {
        SqlSession session = MyBatisUtil.openSession();
        DepartmentMapper mapper = session.getMapper(DepartmentMapper.class);
        Department department = mapper.selectOne(id);
        session.close();

        return department;
    }

    public List<Employee> selectExistEmpsByDeptId(Long deptId) {
        SqlSession session = MyBatisUtil.openSession();
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
        List<Employee> existEmployeeList = employeeMapper.selectExistEmpsByDeptId(deptId);
        session.commit();
        session.close();
        return existEmployeeList;
    }

    public List<Employee> selectNotExistEmpsByDeptId(Long deptId) {
        SqlSession session = MyBatisUtil.openSession();
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
        List<Employee> notExistEmployeeList = employeeMapper.selectNotExistEmpsByDeptId(deptId);
        session.commit();
        session.close();
        return notExistEmployeeList;
    }

    public void bindEmployee(Long deptId, List<Long> empIdList) {
        // 查询已经存在在员工列表
        List<Long> existEmpIdList = selectExistEmpsByDeptId(deptId).stream().map(emp -> emp.getId()).collect(Collectors.toList());
        // 获取新加入的员工 id 列表
        List<Long> newEmpIdList = empIdList.stream().filter(empId -> !existEmpIdList.contains(empId)).collect(Collectors.toList());
        // 获取需要删除部门的员工 id 列表
        List<Long> delEmpList = existEmpIdList.stream().filter(empId -> !empIdList.contains(empId)).collect(Collectors.toList());
        SqlSession session = MyBatisUtil.openSession();
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
        if (newEmpIdList.size() > 0) {
            employeeMapper.updateDept(deptId,newEmpIdList);
        }
        if (delEmpList.size() > 0) {
            employeeMapper.updateDept(null,delEmpList);
        }
        session.commit();
        session.close();
    }
}
